package com.example.kt_fast.ui


import android.util.Log
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.kt_common.base.BaseActivity
import com.example.kt_common.network.BaseStateObserver
import com.example.kt_common.util.Constants
import com.example.kt_common.util.ToastUtil
import com.example.kt_fast.R
import com.example.kt_fast.adapter.CarTreeAdapter
import com.example.kt_fast.bean.TreeNode
import com.example.kt_fast.bean.resp.CarTreeBean
import com.example.kt_fast.databinding.ActivityCarTreeBinding
import com.example.kt_fast.viewmodel.FastViewModel
import org.koin.androidx.viewmodel.ext.android.viewModel

@Route(path = Constants.PATH_CAR_TREE)
class CarTreeActivity : BaseActivity<ActivityCarTreeBinding>() {

    private val carTreeViewModel: FastViewModel by viewModel()

    override fun getLayoutID(): Int {
        return R.layout.activity_car_tree
    }

    override fun init() {
        carTreeViewModel.carTreeData.observe(this, object : BaseStateObserver<CarTreeBean>(null) {
            override fun getRespDataSuccess(it: CarTreeBean) {
                ToastUtil.showMsg("请求 左侧树11.14最终接口 成功")

                Log.d("左侧树11.14最终接口data.label: ", it.toString())
                val id = it.id
                val carStatus = it.carStatus
                val status = it.status//行政区域0  公司1  车队2 车辆3 摄像头4
                val onlineStr = it.onlineStr
                val children = it.children
                val label = it.label
                if (label.isNotEmpty()) {
                    val treeNode = TreeNode(label + "[$carStatus]", status)
                    for (child in children) {
                        populateTree(treeNode, child)
                    }
                    data.add(treeNode)
                    mBind.recyclerView.adapter = CarTreeAdapter(data)

                }
            }
        })
        // 构建测试数据
//        val data = listOf(
//            TreeNode(
//                "动物", children = mutableListOf(
//                    TreeNode(
//                        "哺乳动物", level = 1, children = mutableListOf(
//                            TreeNode("狗", level = 2), TreeNode("猫", level = 2)
//                        )
//                    ), TreeNode(
//                        "鸟类", level = 1, children = mutableListOf(
//                            TreeNode("鹦鹉", level = 2), TreeNode("鹰", level = 2)
//                        )
//                    )
//                )
//            ), TreeNode(
//                "植物", children = mutableListOf(
//                    TreeNode("树木", level = 1), TreeNode("花草", level = 1)
//                )
//            )
//        )
//        mBind.recyclerView.adapter = CarTreeAdapter(data)
        carTreeViewModel.getTreeAside()
    }

    /**
     * 组装数据
     */
    var data: MutableList<TreeNode> = mutableListOf()
    private fun populateTree(treeNode: TreeNode, childrenBean: CarTreeBean.ChildrenBean) {
        val id = childrenBean.id
        val carStatus = childrenBean.carStatus
        val status = childrenBean.status//行政区域0  公司1  车队2 车辆3 摄像头4
        val onlineStr = childrenBean.onlineStr
        val children = childrenBean.children
        val label = childrenBean.label
        if (status != 4) {//(摄像头不用加)
            if (label.isNotBlank()) {
                val treeNodeChildren = TreeNode(label + "[$carStatus]", status)
                treeNode.children.add(treeNodeChildren)
                if (children.isNotEmpty()) {
                    for (child in children) {
                        populateTree(treeNodeChildren, child)
                    }
                }
            }
        }

    }
}